<script type="text/javascript" src="/javascript/jquery/jquery.autoSuggest.js"></script>
<link rel="stylesheet" href="/style/style_asus.css" type="text/css" />
<script type="text/javascript">

/*Настраимаем элементы автозаполнения
 * className - класс div, который содержит input, в этом inpute и будет автозаполнение
 */ 

$(function(){
	 
         $.fn.autosugguest({
            className: 'ausu-suggest',
            methodType: 'POST',
            minChars: 2,
            rtnIDs: true,
            dataFile: '/default/index/product-ajax'
    });
    
  $(".ausu-suggestionsBox > ul li").live("click", function()
    {			

            $('input#user_id').val($(this).attr("id"));
            $('#current_email').text('Выбранный пользователь: '+ $(this).find('a').text());

            return false;
    });
    
    $("#name_product").keyup(function(event)
    {  
        
        if(event.which == 13)
        {           
            $('input#user_id').val($("li[class='selected']").attr("id"));
            $('#current_email').text('Выбранный пользователь: '+ $("li[class='selected']").find('a').text());           
        }                           
    });

});
	
	
</script>

<?php
 
$form = $this->element;

$prod2prod_attr_id = Zend_Controller_Front::getInstance()->getRequest()->getParam('id');

?>
<form  method="post" action="">

	<?php 
	
	/**
     * Translate some form errrors.
     * 
     * @param string $key
     * @param string $error
     * @return string
     */
    function _translate($key, $error)
    {
    	$_translated = array(
			'isEmpty' => 'Поле обязательно для заполнения',
		);
	
    	if (isset($_translated[$key])) {
    		return $_translated[$key];
    	}
    	
    	return $error;
    }
    
    $errors = $form->getMessages();
    foreach ($errors as $key => $error) {
    	if (is_array($error)) {
    		unset($errors[$key]);
    	}

   		if (is_array($error)) {
     		foreach ($error as $k => $v) {
     			$label = $form->getElement($key)->getLabel();
     			$v = _translate($k, $v);
     			$errors[$key][$k] = "$label -- $v";
     		}
    	} else {
    		$errors[$key] = _translate($key, $error);
    	}
    }
    
    if ($errors) {
	    echo "<ul class='errors'>";
	    foreach ($errors as $error) {
	    	if (is_array($error)) {
	    		foreach ($error as $k => $v) {
	    			echo "<li>$v</li>";
	    		}
	    	} else {
	    		echo "<li>$error</li>";
	    	}
	    }
	    echo "</ul>";
    }
	
    if($prod2prod_attr_id)
    {
	 $select = Product2productAttributePeer::getInstance()->select('product_id')->setIntegrityCheck(false) 
                 ->join('product', ProductPeer::ID .'='.Product2productAttributePeer::PRODUCT_ID,array('name'))
                 ->where(Product2productAttributePeer::ID . '= ?',$prod2prod_attr_id);
	//die($select);	
	$row = Product2attributePeer::getInstance()->fetchRow($select);
    }
            
	?>

    
    <div>      	    
        
            <dl class="floater-31">
                <dt id="user_id-label">
                    <label class="required" for="user_id">Товар</label>                        
                </dt>
                
                <dd id="user_id-element" style="display: none;">
                       
                       <input id="user_id" type="text" maxlength="10" size="10" value="<?php if($prod2prod_attr_id) echo $row->product_id ?>" name="product_id"> 
                </dd>
            </dl>
           
        <div id="current_email" style="margin-top: 5px; font-weight: bold; font-style: italic;">Выбранный товар: <?php if($prod2prod_attr_id) echo $row->name; ?></div>
           <div class="ausu-suggest">
                        <input type="text"  size="25" value="<?php if($prod2prod_attr_id) echo $row->name; ?>" name="name_product" id="name_product" autocomplete="off" />
           </div>
           <br clear="all"/>
           
        
        
        
            <dl class="floater-31">
			<?php echo $form->getElement('attribute_id'); ?>
	    </dl>
             <dl class="floater-31">
			<?php echo $form->getElement('attribute_value'); ?>
	    </dl>
            
           
           <dt id="submit-label">&nbsp;</dt>
		<dd id="submit-element">
			<input type="submit" name="submit" id="submit" value="Сохранить" />
		</dd>
    </div>
    
    </form>